package net.xdclass.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.xdclass.enums.CouponCategoryEnum;
import net.xdclass.model.CouponDO;
import net.xdclass.request.NewUserCouponRequest;
import net.xdclass.service.CouponService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-05-19
 */
@Api(tags = "优惠券api")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @ApiOperation("优惠券分页查询")
    @GetMapping("page_coupon")
    public JsonData getPageCoupon(@RequestParam(value = "page",defaultValue = "1") int page,
                                  @RequestParam(value = "size",defaultValue = "10") int size) {
        Map<String,Object> pageInfo = couponService.getPageCoupon(page, size);
        return JsonData.buildSuccess(pageInfo);
    }

    @ApiOperation("领取优惠券")
    @GetMapping("add/promotion/{coupon_id}")
    public JsonData add(@PathVariable("coupon_id") long couponId) {
        JsonData jsonData = couponService.addPromotion(couponId, CouponCategoryEnum.PROMOTION);
        return jsonData;
    }

    @ApiOperation("新用户领取优惠券")
    @PostMapping("new_user_coupon")
    public JsonData addNewUserCoupon(@RequestBody NewUserCouponRequest newUserCouponRequest) {
        JsonData jsonData = couponService.addNewUserCoupon(newUserCouponRequest);
        return jsonData;
    }
}

